{ "err_no": 0, "err_msg": "success", "data": { "booklet": { "booklet_id": "6844733730678898702", "base_info": { "id": 0, "booklet_id": "6844733730678898702", "title": "基于 Go 语言构建企业级的 RESTful API 服务", "price": 3990, "category_id": "6809637769959178254", "status": 1, "user_id": "2541726582714526", "verify_status": 3, "summary": "Go 服务器开发大型实战,带你一步步构建 API 开发中的各个功能点,最终完成一个企业级的 API 服务器", "cover_img": "https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/6/26/1643b682a5be1090~tplv-t2oaga2asx-image.image", "section_count": 27, "section_ids": "6844733730683093000|6844733730704080910|6844733730720841742|6844733730720841735|6844733730725036045|6844733730725036039|6844733730725036040|6844733730729230343|6844733730729230350|6844733730729246727|6844733730733424653|6844733730733441037|6844733730733457421|6844733730733441032|6844733730737635335|6844733730737782792|6844733730737618958|6844733730741813255|6844733730741829639|6844733730741813262|6844733730746007566|6844733730746007565|6844733730746023949|6844733730750201864|6844733730750365709|6844733730750201863|6844733730754396167|6844733730754396174", "is_finished": 1, "ctime": 1527216245, "mtime": 1644916220, "put_on_time": 1598288804, "pull_off_time": 1644916219, "finished_time": 1596106628, "recycle_bin_time": -62135596800, "verify_time": -62135596800, "submit_time": 1639378746, "top_time": -62135596800, "wechat_group_img": "https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/1573387211392902edb2099efd2887903496d5ee833b4.jpg~tplv-t2oaga2asx-image.image", "wechat_group_desc": "小册二姐", "wechat_group_signal": "lexkong3668", "read_time": 12237, "buy_count": 3777, "course_type": 1, "background_img": "https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c76e37630ba44b898afb14375d85e54f~tplv-k3u1fbpfcp-watermark.image?", "is_distribution": 0, "distribution_img": "", "commission": 798, "can_vip_borrow": false, "is_sign": false }, "reading_progress": { "id": 0, "booklet_id": "6844733730678898702", "user_id": "3210229681503629", "status": 1, "buy_type": 1, "reading_end": 0, "reading_progress": 0, "last_section_id": "0", "has_update": 1, "last_rtime": 1530106209, "ctime": 1530106209, "mtime": 1658224502, "valid_begin_time": 0, "valid_end_time": 0, "borrow_times": 0 }, "user_info": { "user_id": "2541726582714526", "user_name": "雷克斯", "company": "腾讯科技", "job_title": "高级后台开发工程师", "avatar_large": "https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/6/26/1643aaeef14b83e9~tplv-t2oaga2asx-image.image", "level": 0, "description": "Go;Shell; Docker;Kubernetes;微服务;容器云", "followee_count": 6, "follower_count": 180, "post_article_count": 0, "digg_article_count": 0, "got_digg_count": 0, "got_view_count": 0, "post_shortmsg_count": 0, "digg_shortmsg_count": 0, "isfollowed": false, "favorable_author": 0, "power": 0, "study_point": 0, "university": { "university_id": "0", "name": "", "logo": "" }, "major": { "major_id": "0", "parent_id": "0", "name": "" }, "student_status": 0, "select_event_count": 0, "select_online_course_count": 0, "identity": 0, "is_select_annual": false, "select_annual_rank": 0, "annual_list_type": 0, "extraMap": {}, "is_logout": 0, "annual_info": [], "account_amount": 0, "user_growth_info": { "user_id": 2541726582714526, "jpower": 0, "jscore": 6.7, "jpower_level": 0, "jscore_level": 1, "jscore_title": "预备掘友", "author_achievement_list": [], "vip_level": 0, "vip_title": "" }, "is_vip": false }, "event_discount": null, "is_buy": true, "section_updated_count": 27, "is_new": false }, "introduction": { "id": 80820, "section_id": "6844733730683093000", "title": "介绍", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "

教程介绍

\n

现代软件架构已经逐渐从单体架构转向微服务架构,在微服务架构中服务间通信采用轻量级通信机制。对于轻量级通信的协议而言,通常基于 HTTP 和 RPC ,能让服务间的通信变的标准化并且无状态化。此外开发模式也越来越多的采用前后端分离的模式,在前后端分离的模式中,前后端通信一般是通过 HTTP 进行通信。

\n

不管是微服务架构,还是前后端分离模式,都需要一个 HTTP API 服务器。而且在日后的开发生涯中可能需要构建很多个大大小小的 API 服务器,所以很多时候做 Go 语言后台开发其实就是做 API 开发。有很多种方法可以用来构建 API,很多企业构建 API 时,采用一种叫 REST 风格的方式来构建 API,它虽然调用性能不及 RPC,但维护性和扩展性更好,也更通用。由于本教程不讨论微服务之间的高频调用场景, 而 REST 在实际开发中,能够满足绝大部分的需求场景,基于它的其他优势,本教程采用 REST 风格来构建 API 服务器。此外,在媒体类型上选择了 JSON,因为它的内容更加紧凑,数据展现形式直观易懂,开发测试都非常方便。REST + JSON,这也是 Go API 开发中很常用的组合。

\n

构建一个简单的 API 服务器很简单,但构建一个生产就绪的 API 服务还有很多工作要做。所谓的生产就绪,至少需要满足如下各方面:

\n
    \n
  1. 需要读取配置文件、记录日志
  2. \n
  3. 需要连接数据库
  4. \n
  5. 需要对数据库做增删改查等操作
  6. \n
  7. 需要自定义业务错误码
  8. \n
  9. 需要进行 API 身份验证
  10. \n
  11. 需要给 API 增加 Swagger 文档
  12. \n
  13. API 服务器需要满足高稳定性,高性能的要求
  14. \n
  15. API 需要做高可用
  16. \n
  17. ....
  18. \n
\n

可以看到要构建一个可以运行在生产环境中的 API 服务器有许多工作要做。本教程希望花尽可能短的时间,来教初学者了解和学习 Go API 开发的所有环节和功能点。构建中需要用到很多 Go 包,笔者也根据经验筛选出了一些非常优秀的 Go 包来使用,另外教程中很多地方也会附上笔者的开发经验和建议,希望对读者有一定的帮助。

\n

你会学到什么

\n

本教程是一个实战类的教程,旨在让初学者花尽可能短的时间,通过尽可能详细的步骤,历经 17 个 demo,最终一步步构建出一个生产级的 API 服务器。从开发准备到 API 设计,再到 API 实现、测试和部署,每一步都详细介绍了如何去构建。通过本教程的学习,你将学到如下知识点:

\n

\"image.png\"

\n

知识点很多,跟着教程一节一节进行学习,你将完整的学会如何用 Go 做 API 开发。

\n

适宜人群

\n\n

你应该具备什么

\n", "draft_content": "", "draft_title": "介绍", "markdown_content": "", "markdown_show": "", "is_free": 1, "read_time": 132, "read_count": 0, "comment_count": 0, "ctime": 1527216245, "mtime": 1546606772, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, "sections": [ { "id": 75446, "section_id": "6844733730704080910", "title": "本小册所实现的 API 功能", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "本小册所实现的 API 功能", "markdown_content": "", "markdown_show": "", "is_free": 1, "read_time": 135, "read_count": 4649, "comment_count": 51, "ctime": 1527595310, "mtime": 1594606994, "is_update": 0, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75447, "section_id": "6844733730720841742", "title": "RESTful API 介绍", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "RESTful API 介绍", "markdown_content": "", "markdown_show": "", "is_free": 1, "read_time": 269, "read_count": 3651, "comment_count": 45, "ctime": 1527595346, "mtime": 1594211190, "is_update": 0, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75449, "section_id": "6844733730720841735", "title": "API 流程和代码结构", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "API 流程和代码结构", "markdown_content": "", "markdown_show": "", "is_free": 1, "read_time": 458, "read_count": 4063, "comment_count": 19, "ctime": 1527597270, "mtime": 1588401913, "is_update": 0, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75448, "section_id": "6844733730725036045", "title": "Go API 开发环境配置", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "Go API 开发环境配置", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 293, "read_count": 2781, "comment_count": 23, "ctime": 1527596651, "mtime": 1595493680, "is_update": 0, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75450, "section_id": "6844733730725036039", "title": "基础 1:启动一个最简单的 RESTful API 服务器", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 1:启动一个最简单的 RESTful API 服务器", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 887, "read_count": 3398, "comment_count": 101, "ctime": 1527597276, "mtime": 1587915888, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75471, "section_id": "6844733730725036040", "title": "基础 2:配置文件读取", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 2:配置文件读取", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 1199, "read_count": 2204, "comment_count": 54, "ctime": 1527671781, "mtime": 1588415583, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75486, "section_id": "6844733730729230343", "title": "基础 3:记录和管理 API 日志", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 3:记录和管理 API 日志", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 602, "read_count": 1647, "comment_count": 30, "ctime": 1527695453, "mtime": 1595385948, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75494, "section_id": "6844733730729230350", "title": "基础 4:安装 MySQL 并初始化表", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 4:安装 MySQL 并初始化表", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 821, "read_count": 1419, "comment_count": 12, "ctime": 1527752868, "mtime": 1550220340, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75488, "section_id": "6844733730729246727", "title": "基础 5:初始化 MySQL 数据库并建立连接", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 5:初始化 MySQL 数据库并建立连接", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 389, "read_count": 1449, "comment_count": 27, "ctime": 1527696938, "mtime": 1595572450, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75491, "section_id": "6844733730733424653", "title": "基础 6:自定义业务错误信息", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 6:自定义业务错误信息", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 957, "read_count": 1521, "comment_count": 20, "ctime": 1527752501, "mtime": 1577782245, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75492, "section_id": "6844733730733441037", "title": "基础 7:读取和返回 HTTP 请求", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 7:读取和返回 HTTP 请求", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 832, "read_count": 1305, "comment_count": 14, "ctime": 1527752517, "mtime": 1595923628, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75511, "section_id": "6844733730733457421", "title": "基础 8:用户业务逻辑处理", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 8:用户业务逻辑处理", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 1244, "read_count": 1404, "comment_count": 83, "ctime": 1527906666, "mtime": 1595580101, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75513, "section_id": "6844733730733441032", "title": "基础 9:HTTP 调用添加自定义处理逻辑", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 9:HTTP 调用添加自定义处理逻辑", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 611, "read_count": 941, "comment_count": 15, "ctime": 1528018585, "mtime": 1584845933, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75560, "section_id": "6844733730737635335", "title": "基础 10:API 身份验证", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "基础 10:API 身份验证", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 708, "read_count": 1280, "comment_count": 33, "ctime": 1528324663, "mtime": 1578555126, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75561, "section_id": "6844733730737782792", "title": "进阶 1:用 HTTPS 加密 API 请求", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 1:用 HTTPS 加密 API 请求", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 530, "read_count": 822, "comment_count": 18, "ctime": 1528324689, "mtime": 1591325207, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75562, "section_id": "6844733730737618958", "title": "进阶 2:用 Makefile 管理 API 项目", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 2:用 Makefile 管理 API 项目", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 305, "read_count": 929, "comment_count": 8, "ctime": 1528324715, "mtime": 1595833173, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75563, "section_id": "6844733730741813255", "title": "进阶 3:给 API 命令增加版本功能", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 3:给 API 命令增加版本功能", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 570, "read_count": 793, "comment_count": 8, "ctime": 1528324743, "mtime": 1584955799, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75564, "section_id": "6844733730741829639", "title": "进阶 4:给 API 增加启动脚本", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 4:给 API 增加启动脚本", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 275, "read_count": 779, "comment_count": 6, "ctime": 1528324826, "mtime": 1571803511, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75565, "section_id": "6844733730741813262", "title": "进阶 5:基于 Nginx 的 API 部署方案", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 5:基于 Nginx 的 API 部署方案", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 927, "read_count": 875, "comment_count": 16, "ctime": 1528324840, "mtime": 1579080801, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75566, "section_id": "6844733730746007566", "title": "进阶 6:API 高可用方案", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 6:API 高可用方案", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 111, "read_count": 847, "comment_count": 4, "ctime": 1528324857, "mtime": 1595835329, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75567, "section_id": "6844733730746007565", "title": "进阶 7:go test 测试你的代码", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 7:go test 测试你的代码", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 851, "read_count": 798, "comment_count": 6, "ctime": 1528324878, "mtime": 1595836389, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75568, "section_id": "6844733730746023949", "title": "进阶 8:API 性能分析", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 8:API 性能分析", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 321, "read_count": 776, "comment_count": 3, "ctime": 1528324893, "mtime": 1548040554, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75569, "section_id": "6844733730750201864", "title": "进阶 9:生成 Swagger 在线文档", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 9:生成 Swagger 在线文档", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 511, "read_count": 962, "comment_count": 15, "ctime": 1528324906, "mtime": 1584257497, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75654, "section_id": "6844733730750365709", "title": "进阶 10:API 性能测试和调优", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "进阶 10:API 性能测试和调优", "markdown_content": "", "markdown_show": "", "is_free": 1, "read_time": 1443, "read_count": 1600, "comment_count": 9, "ctime": 1529750885, "mtime": 1571494163, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75570, "section_id": "6844733730750201863", "title": "拓展 1:Go 开发技巧", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "拓展 1:Go 开发技巧", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 47, "read_count": 954, "comment_count": 1, "ctime": 1528324924, "mtime": 1590751346, "is_update": 1, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75571, "section_id": "6844733730754396167", "title": "拓展 2:Go 规范指南", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "拓展 2:Go 规范指南", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 587, "read_count": 918, "comment_count": 5, "ctime": 1528324936, "mtime": 1590751641, "is_update": 0, "draft_read_time": 0, "vid": "", "reading_progress": null }, { "id": 75572, "section_id": "6844733730754396174", "title": "总结", "user_id": "2541726582714526", "booklet_id": "6844733730678898702", "status": 1, "content": "", "draft_content": "", "draft_title": "总结", "markdown_content": "", "markdown_show": "", "is_free": 0, "read_time": 21, "read_count": 581, "comment_count": 42, "ctime": 1528324947, "mtime": 1588914935, "is_update": 0, "draft_read_time": 0, "vid": "", "reading_progress": null } ] } }